package com.sgb.kjwl.pop;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.view.widget.SelectDialog;

import java.util.ArrayList;
import java.util.List;

public class ChoiceMorePop extends Dialog {
    public ChoiceMorePop(Context context, int themeResId) {
        super(context, themeResId);
    }
    public static class Params {
        private Context context;
    }
    public static class Builder<T> {
        private boolean canCancel = true;
        private boolean shadow = true;
        private final Params p;
        private List<T> listdata = new ArrayList<>();
        private View.OnClickListener lister;
        private String title;

        public Builder(Context context) {
            p = new Params();
            p.context = context;
        }
        public Builder setListData(List<T> listdata ){
            this.listdata = listdata;
            return this;
        }
        public Builder setListOnclickLister(View.OnClickListener lister){
            this.lister = lister;
            return this;
        }
        public Builder setTitle(String title){
            this.title = title;
            return this;
        }
        public ChoiceMorePop create() {
            final ChoiceMorePop dialog = new ChoiceMorePop(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            Window window = dialog.getWindow();
            window.setWindowAnimations(R.style.Animation_Design_BottomSheetDialog);
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            window.setGravity(Gravity.BOTTOM);
            View view = LayoutInflater.from(p.context).inflate(R.layout.popwindow_choicemore, null);
            TextView tv_title = view.findViewById(R.id.tv_title);
            TextView tv_lefttext = view.findViewById(R.id.tv_lefttext);
            TextView tv_ok = view.findViewById(R.id.tv_ok);
            RecyclerView recyclerview = view.findViewById(R.id.recyclerview);
            recyclerview.setLayoutManager(new LinearLayoutManager(p.context));
            recyclerview.setAdapter(new MyAdpater());
            if(!TextUtils.isEmpty(title)){
                tv_title.setText(title);
            }else{
                tv_title.setText("");
            }
            tv_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(lister!=null){
                        lister.onClick(v);
                    }
                    dialog.dismiss();
                }
            });
            tv_lefttext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);
            return dialog;
        }
        public class  MyAdpater extends RecyclerView.Adapter<MyAdpater.MViewHolder>{
            @NonNull
            @Override
            public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(p.context).inflate(R.layout.item_choicemore,parent,false);
                return new MViewHolder(view);
            }
            @Override
            public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
                holder.setData(listdata.get(position),position);
            }
            @Override
            public int getItemCount() {
                return listdata.size();
            }
            public class  MViewHolder extends RecyclerView.ViewHolder{
                TextView tv_text;
                ImageView image_choice;
                public MViewHolder(@NonNull View itemView) {
                    super(itemView);
                    tv_text= itemView.findViewById(R.id.tv_text);
                    image_choice = itemView.findViewById(R.id.image_choice);
                }
                public void setData(T data,int position){
                    if(data instanceof ChoiceData){
                        tv_text.setText(((ChoiceData)data).getText());
                        if(((ChoiceData)data).getChoice()){
                            tv_text.setTextColor(Color.parseColor("#5792FD"));
                            image_choice.setVisibility(View.VISIBLE);
                        }else{
                            tv_text.setTextColor(Color.parseColor("#333333"));
                            image_choice.setVisibility(View.INVISIBLE);
                        }
                        itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ((ChoiceData)data).setChoice(!((ChoiceData)data).getChoice());
                                notifyDataSetChanged();
                            }
                        });
                    }
                }
            }
        }
    }
    public interface ChoiceData{
        public String getText();
        public Boolean getChoice();
        public void setChoice(Boolean choice);
    }
}

